﻿namespace DotNetUtility.ExtensionMethods
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using DotNetUtility.ExtensionMethods.Generics;
    using System.Data;
    /// <summary>
    /// 
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="itemLength"></param>
        /// <returns></returns>
        public static IEnumerable<string> IfLengthEquals(this IEnumerable<string> myList, int itemLength)
        {
            foreach (var item in myList.Where(item => item.Length == itemLength))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="startOfRange"></param>
        /// <param name="endOfRange"></param>
        /// <returns></returns>
        public static IEnumerable<string> IfLengthInRange(this IEnumerable<string> myList, int startOfRange, int endOfRange)
        {
            foreach (var item in myList.Where(item => item.Length >= startOfRange || item.Length <= endOfRange))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="itemToMatch"></param>
        /// <returns></returns>
        public static IEnumerable<string> IfMatchWith(this IEnumerable<string> myList, string itemToMatch)
        {
            foreach (var item in myList.Where(item => item == itemToMatch))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static IEnumerable<string> IfMatchWithPattern(this IEnumerable<string> myList, string pattern)
        {
            foreach (var item in myList.Where(item => Regex.IsMatch(item, pattern)))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="itemToMatch"></param>
        /// <returns></returns>
        public static IEnumerable<string> IfNotMatchWith(this IEnumerable<string> myList, string itemToMatch)
        {
            foreach (var item in myList.Where(item => item != itemToMatch))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <returns></returns>
        public static IEnumerable<string> IgnoreNullOrEmptyOrSpace(this IEnumerable<string> myList)
        {
            foreach (var item in myList.Where(item => !string.IsNullOrEmpty(item) && item != " "))
                yield return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="myList"></param>
        /// <returns></returns>
        public static IEnumerable<T> MakeAllDefault<T>(this IEnumerable<T> myList)
        {
            foreach (var item in myList)
                yield return default(T);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <returns></returns>
        public static IEnumerable<string> MakeAllLower(this IEnumerable<string> myList)
        {
            foreach (var item in myList)
                yield return item.ToLower();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myList"></param>
        /// <returns></returns>
        public static IEnumerable<string> MakeAllUpper(this IEnumerable<string> myList)
        {
            foreach (var item in myList)
                yield return item.ToUpper();
        }

        /// <summary>
        /// This method extends IEnumerable to return an ICollection so we can get a count. It makes
        /// use of a generic Cast extension method to make the code more fluent.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static ICollection<T> ToICollection<T>(this IEnumerable<T> collection)
        {
            return collection.ToList<T>().Cast<IList<T>, ICollection<T>>();
        }
    }
}
